package com.lanyou.cook.repository.userManagement;


import java.util.List;

import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.Repository;
import org.springframework.data.repository.query.Param;
import org.springframework.data.rest.core.annotation.RestResource;

import com.lanyou.cook.entity.domain.userManagement.Organization;

public interface OrganizationRepository extends Repository<Organization, String>, JpaSpecificationExecutor<Organization> {
	

	//组织结构的展现
	@RestResource(exported = false)
	List<Organization> findByParent_Id(String parentId);
	
	
	
	//组织结构的展现
	@RestResource(exported = false)
	List<Organization> findByLevel(@Param("level") int level);
	
	//组织结构的展现
	@RestResource(exported = false)
	Organization findByOrgName(@Param("name") String name);
	
	@RestResource(exported = false)
	Organization findByOrgNameAndLevel(@Param("name") String name,@Param("level")int level);
	
	@Query(nativeQuery = true,
			value = "SELECT * FROM t_cook_bu_organization"
					+ " WHERE (ORG_NAME = '' OR  ORG_NAME = :name )"
					+ "AND (PARENT_ORG_ID = '' OR PARENT_ORG_ID = :parent)")
	Organization findByOrgNameAndParent(@Param("name")String name,
										@Param("parent")String parent);
	
	
	@RestResource(exported = false)
	int countByParent_id(String id);
	
	
	long count();
	
	boolean exists(String id);
	
	@RestResource(exported = true)
//	@RequiresPermissions(value={"organizationManagement:maintain"})
	<S extends Organization> S save(S entity);	// could expose REST API
	
	<S extends Organization> List<S> save(Iterable<S> entities);
	
	@RestResource(exported = false)
	Organization findOne(String id);	// could expose REST API
	
	Organization getOne(String id);
	
	// 以下三个接口只能选一个暴露为REST API
	
	@RestResource(exported = false)
	List<Organization> findAll();	// could expose REST API
	
	@RestResource(exported = false)
	List<Organization> findAll(Sort sort);	// could expose REST API
	
	@RestResource(exported = true)
//	@RequiresPermissions(value={"organizationManagement:view","organizationManagement:maintain"},logical=Logical.OR)
	Page<Organization> findAll(Pageable pageable);	// could expose REST API
	
	List<Organization> findAll(Iterable<String> ids);
	
	// 以下两个接口，要么都暴露为REST API，要么都不暴露
	@RestResource(exported = true)
//	@RequiresPermissions(value={"organizationManagement:maintain"})
	void delete(String id);	// could expose REST API
	
	@RestResource(exported = true)
//	@RequiresPermissions(value={"organizationManagement:maintain"})
	void delete(Organization entity);	// could expose REST API
	
	void delete(Iterable<? extends Organization> entities);
	
	void deleteAll();
	
	void flush();
	
	<S extends Organization> S saveAndFlush(S entity);
	
	void deleteInBatch(Iterable<Organization> entities);
	
	void deleteAllInBatch();
	
	
}
